package com.example.sudokugame

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.sudokugame.databinding.ActivityMainBinding
import com.example.sudokugame.models.Difficulty
import com.example.sudokugame.network.NetworkManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    
    private lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        
        setupClickListeners()
        checkContinueGameAvailable()
        checkServerConnection()
    }
    
    private fun setupClickListeners() {
        binding.btnNewGame.setOnClickListener {
            showDifficultyDialog()
        }
        
        binding.btnContinueGame.setOnClickListener {
            // TODO: 实现继续游戏功能
            Toast.makeText(this, "继续游戏功能开发中", Toast.LENGTH_SHORT).show()
        }
        
        binding.btnLeaderboard.setOnClickListener {
            startActivity(Intent(this, LeaderboardActivity::class.java))
        }
        
        binding.btnSettings.setOnClickListener {
            // TODO: 实现设置功能
            Toast.makeText(this, "设置功能开发中", Toast.LENGTH_SHORT).show()
        }
    }
    
    private fun showDifficultyDialog() {
        val difficulties = arrayOf("简单", "中等", "困难", "专家")
        val difficultyValues = arrayOf(
            Difficulty.EASY,
            Difficulty.MEDIUM,
            Difficulty.HARD,
            Difficulty.EXPERT
        )
        
        MaterialAlertDialogBuilder(this)
            .setTitle("选择难度")
            .setItems(difficulties) { _, which ->
                startNewGame(difficultyValues[which])
            }
            .setNegativeButton("取消", null)
            .show()
    }
    
    private fun startNewGame(difficulty: Difficulty) {
        val intent = Intent(this, GameActivity::class.java).apply {
            putExtra("difficulty", difficulty.value)
        }
        startActivity(intent)
    }
    
    private fun checkContinueGameAvailable() {
        // TODO: 检查是否有保存的游戏进度
        // 暂时禁用继续游戏按钮
        binding.btnContinueGame.isEnabled = false
    }
    
    private fun checkServerConnection() {
        lifecycleScope.launch {
            try {
                val response = NetworkManager.apiService.healthCheck()
                if (response.isSuccessful) {
                    // 服务器连接正常，启用在线功能
                    binding.btnLeaderboard.isEnabled = true
                } else {
                    // 服务器连接失败，禁用在线功能
                    binding.btnLeaderboard.isEnabled = false
                    Toast.makeText(this@MainActivity, "服务器连接失败，排行榜功能不可用", Toast.LENGTH_LONG).show()
                }
            } catch (e: Exception) {
                // 网络错误，禁用在线功能
                binding.btnLeaderboard.isEnabled = false
                Toast.makeText(this@MainActivity, "网络连接失败，排行榜功能不可用", Toast.LENGTH_LONG).show()
            }
        }
    }
} 